package com.goodving.guli.bt.profile.scale;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.os.Environment;

import com.goodving.guli.bt.model.C3918p;
import com.goodving.guli.view.C4220a;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;

/**
 * Created by john on 2015/11/25.
 */
public class GattPeripheral extends AbsGattCallback { // C3891x
    public static final int f19554a = 0;
    public static final int f19555b = 1;
    public static final int f19556c = 2;
    public static final int f19557d = 3;
    public static final int f19558e = 4;
    private static final String f19559k = "GattPeripheral";
    protected int d_;
    private boolean f19560l;
    private C1724A f19561m;
    private FileOutputStream f19562n;
    private FileLock f19563o;

    protected GattPeripheral(Context context, BluetoothDevice bluetoothDevice, C1724A c1724a) {
        super(context, bluetoothDevice);
        this.d_ = f19554a;
        this.f19560l = true;
        this.f19561m = null;
        this.f19562n = null;
        this.f19563o = null;
        GuliLog.m12371e();
        this.f19561m = c1724a;
    }

    private void __lockFile() { // m25288y
        GuliLog.m12371e();
        try {
            this.f19562n = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Android/.btlock");
            this.f19563o = this.f19562n.getChannel().lock();
            GuliLog.m12350a("FileLock: " + this.f19563o.isValid());
            this.f19562n.write(f19555b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    private void __releaseFile () { // m25289z
        GuliLog.m12371e();
        if (this.f19562n != null && this.f19563o != null) {
            try {
                this.f19562n.write(f19554a);
                this.f19563o.release();
                this.f19562n.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    protected void GattCB_onReadRemoteRssi(int i) { // m25264a
        GuliLog.m12371e();
    }

    protected void GattCB_onDescriptorRead(BluetoothGattDescriptor bluetoothGattDescriptor) { // m25266a
        GuliLog.m12371e();
    }

    public void m25292a(Object obj) {
        if (obj != null) {
            synchronized (obj) {
                obj.notify();
            }
        }
    }

    public void m25293a(Object obj, int i) {
        if (obj != null) {
            synchronized (obj) {
                try {
                    obj.wait((long) i);
                } catch (Exception e) {
                }
            }
        }
    }

    public void m25294a(boolean z) {
        this.f19560l = z;
    }

    public boolean m25295a(int i, int i2, int i3, int i4, int i5) {
        return false;
    }

    protected void GattCB_onDescriptorWrite(BluetoothGattDescriptor bluetoothGattDescriptor) { // m25269b
        GuliLog.m12371e();
    }

    public boolean write(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) { // m25297b
        GuliLog.m12371e();
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGattCharacteristic);
        GuliLog.ASSERT_NOT_NULL((Object) bArr);
        if (bluetoothGattCharacteristic == null || bArr == null) {
            return false;
        }
        int a = writeCharacteristic(bluetoothGattCharacteristic, bArr);
        GuliLog.ASSERTION_TRUE(a == 0);
        return a == 0;
    }

    public /* bridge */ /* synthetic */ void m25298c() {
        super.m25271c();
    }

    protected void GattCB_onCharacteristicChanged(BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25274d
        GuliLog.m12371e();
    }

    protected void GattCB_onCharacteristicRead(BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25276e
        GuliLog.m12371e();
    }

    protected void GattCB_onCharacteristicWrite(BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25278f
        GuliLog.m12371e();
    }

    @SuppressLint({"NewApi"})
    public byte[] read(BluetoothGattCharacteristic bluetoothGattCharacteristic) { // m25302g
        GuliLog.m12371e();
        GuliLog.ASSERT_NOT_NULL((Object) bluetoothGattCharacteristic);
        if (bluetoothGattCharacteristic == null) {
            return null;
        }
        int a = readCharacteristic(bluetoothGattCharacteristic);
        GuliLog.ASSERTION_TRUE(a == 0);
        return a == 0 ? bluetoothGattCharacteristic.getValue() : null;
    }

    @SuppressLint({"NewApi"})
    protected void GattCB_onConnected() { // m25280h
        GuliLog.m12371e();
        if (this.f19561m != null) {
            this.f19561m.onDeviceConnected(m25277f());
        }
        boolean discoverServices = m25284l().discoverServices();
        GuliLog.m12370d(f19559k, "discoverServices return " + discoverServices);
        if (!discoverServices) {
            cleanupInternal();
        }
    } // m25303h

    protected void GattCB_onDisconnected() { // m25281i
        GuliLog.m12371e();
        if (this.f19561m != null) {
            this.f19561m.onDeviceDisconnected(m25277f());
        }
        cleanupInternal();
    } //m25304i

    protected void GattCB_onConnectionFailed() { // m25282j
        GuliLog.m12371e();
        if (this.f19561m != null) {
            this.f19561m.onDeviceConnectionFailed(m25277f());
        }
        cleanupInternal();
    }  //m25305j

    @SuppressLint({"NewApi"})
    protected void GattCB_onServiceDiscovered() { //m25283k m25306k
        GuliLog.m12371e();
        for (BluetoothGattService bluetoothGattService : m25284l().getServices()) {
            GuliLog.m12350a((bluetoothGattService.getType() == 0 ? "Primary" : "Secondary") + " service: " + GattUtils.m25341c(bluetoothGattService.getUuid()));
            for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattService.getCharacteristics()) {
                GuliLog.m12350a("  Characteristic: " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
                GuliLog.m12350a("    - Properties: " + GattUtils.m25334b(bluetoothGattCharacteristic.getProperties()));
                for (BluetoothGattDescriptor uuid : bluetoothGattCharacteristic.getDescriptors()) {
                    GuliLog.m12350a("    Descriptor: " + GattUtils.m25341c(uuid.getUuid()));
                }
            }
        }
        boolean s = initBtDev();
        GuliLog.ASSERTION_TRUE(s);
        if (!s) {
            disconnectInternal();
        }
    }

    @SuppressLint({"NewApi"})
    protected final void connectInternal() { // m25285m m25307m
        GuliLog.m12371e();
        GuliLog.m12350a("device:");
        GuliLog.m12350a("         name: " + m25277f().getName());
        GuliLog.m12350a("      address: " + m25277f().getAddress());
        switch (m25277f().getBondState()) {
            case C4220a.bd /*10*/:
                GuliLog.m12350a("   bond state: NONE");
                break;
            case C4220a.aW /*11*/:
                GuliLog.m12350a("   bond state: BONDING");
                break;
            case C4220a.be /*12*/:
                GuliLog.m12350a("   bond state: BONDED");
                break;
        }
        switch (m25277f().getType()) {
            case f19554a /*0*/:
                GuliLog.m12350a("         type: UNKNOWN");
                break;
            case f19555b /*1*/:
                GuliLog.m12350a("         type: CLASSIC");
                break;
            case f19556c /*2*/:
                GuliLog.m12350a("         type: LE");
                break;
            case f19557d /*3*/:
                GuliLog.m12350a("         type: DUAL");
                break;
        }
        super.connectInternal();
    }

    protected final void disconnectInternal() { // m25308n
        GuliLog.m12371e();
        super.disconnectInternal();
    }

    protected void cleanupInternal() {  // m25309o
        GuliLog.m12371e();
        m25316w();
        super.cleanupInternal();
        int state = BluetoothAdapter.getDefaultAdapter().getState();
        GuliLog.WARN("BluetoothAdapter state:" + state);
        if (!this.f19560l) {
            return;
        }
        if (state == 12 || state == 11) {
            try {
                GuliLog.m12350a("Delay 1 second before reconnect...");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
            connectInternal();
        }
    }

    public final boolean m25310q() {
        return this.f19560l;
    }

    public void m25311r() {
        this.f19561m = null;
    }

    public boolean initBtDev() { // m25312s
        GuliLog.m12371e();
        boolean t = initCharacteristics();
        GuliLog.ASSERTION_TRUE(t);
        if (t) {
            GuliLog.INFO("=================================================");
            GuliLog.INFO("============= INITIALIZATION SUCCESS ============");
            GuliLog.INFO("=================================================");
            this.d_ = f19555b;
            if (this.f19561m != null) {
                this.f19561m.onInitializationSuccess(m25277f());
            }
            return true;
        } else {
            GuliLog.ERROR("=================================================");
            GuliLog.ERROR("============= INITIALIZATION FAILED =============");
            GuliLog.ERROR("=================================================");
            this.d_ = f19556c;
            if (this.f19561m != null) {
                this.f19561m.onInitializationFailed(m25277f());
            }
            return false;
        }
    }

    protected boolean initCharacteristics() { // m25313t
        return true;
    }

    public boolean m25314u() {
        return this.d_ == f19555b && m25279g() == XMBLEMSTATUS.CONNECTED;
    }

    public boolean m25315v() {
        return this.d_ == f19555b && m25279g() == XMBLEMSTATUS.CONNECTED;
    }

    public void m25316w() {
        GuliLog.m12371e();
    }

    public C3918p m25317x() {
        return null;
    }
}
